package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.StudentVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Grades;
import com.ruoyi.system.service.IGradesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 年级Controller
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/system/grades")
public class GradesController extends BaseController
{
    @Autowired
    private IGradesService gradesService;

    /**
     * 查询年级列表
     */
//    @PreAuthorize("@ss.hasPermi('system:grades:list')")
    @GetMapping("/list")
    public TableDataInfo list(Grades grades)
    {
        startPage();
        List<Grades> list = gradesService.selectGradesList(grades);
        return getDataTable(list);
    }

    /**
     * 根据登录学校查到所有年级
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:grades:query')")
    @GetMapping("/selectGradesToSid")
    public AjaxResult selectGradesToSid() {
        return AjaxResult.success(gradesService.selectGradesToSid());
    }

    /**
     * 导出年级列表
     */
    @PreAuthorize("@ss.hasPermi('system:grades:export')")
    @Log(title = "年级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Grades grades)
    {
        List<Grades> list = gradesService.selectGradesList(grades);
        ExcelUtil<Grades> util = new ExcelUtil<Grades>(Grades.class);
        util.exportExcel(response, list, "年级数据");
    }

    /**
     * 用户分班,导出整个年级的学生信息
     * @param response
     */
    @PreAuthorize("@ss.hasPermi('system:grades:export')")
    @Log(title = "年级", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAllStudent")
    public void exportAllStudent(HttpServletResponse response, Integer gid) {
        List<StudentVo> studentVos = gradesService.exportAllStudent(gid);
        Grades grades = gradesService.selectGradesByGid(Long.valueOf(gid));
        ExcelUtil<StudentVo> util = new ExcelUtil<>(StudentVo.class);
        util.exportExcel(response, studentVos, grades.getGradeName() + "下学生信息");
    }

    /**
     * 获取年级详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:grades:query')")
    @GetMapping(value = "/{gid}")
    public AjaxResult getInfo(@PathVariable("gid") Long gid)
    {
        return success(gradesService.selectGradesByGid(gid));
    }

    /**
     * 新增年级
     */
    @PreAuthorize("@ss.hasPermi('system:grades:add')")
    @Log(title = "年级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Grades grades)
    {
        return toAjax(gradesService.insertGrades(grades));
    }

    /**
     * 修改年级
     */
    @PreAuthorize("@ss.hasPermi('system:grades:edit')")
    @Log(title = "年级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Grades grades)
    {
        return toAjax(gradesService.updateGrades(grades));
    }

    /**
     * 删除年级
     */
    @PreAuthorize("@ss.hasPermi('system:grades:remove')")
    @Log(title = "年级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gids}")
    public AjaxResult remove(@PathVariable Long[] gids)
    {
        return toAjax(gradesService.deleteGradesByGids(gids));
    }
}
